package com.rwind.dynamic.drill;

import org.apache.drill.common.config.DrillConfig;
import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.server.Drillbit;
import org.apache.drill.exec.server.RemoteServiceSet;
import org.apache.drill.exec.store.SchemaConfig;
import org.apache.drill.exec.store.StoragePluginRegistry;
import org.apache.drill.exec.store.sys.PersistentStoreProvider;

public class AddStorage {
  /**
   * Class.forName("org.h2.Driver");
   *  String connString = String.format(
   *    "jdbc:h2:%s", dirTestWatcher.getTmpDir().getCanonicalPath());
   *  try (Connection connection = DriverManager.getConnection(connString, "root", "root")) {
   *   URL scriptFile = TestJdbcPluginWithH2IT.class.getClassLoader().getResource("h2-test-data.sql");
   *   Assert.assertNotNull("Script for test tables generation 'h2-test-data.sql' " +
   *     "cannot be found in test resources", scriptFile);
   *   RunScript.execute(connection, new FileReader(scriptFile.getFile()));
   *  }
   *  startCluster(ClusterFixture.builder(dirTestWatcher));
   *  JdbcStorageConfig jdbcStorageConfig = new JdbcStorageConfig(
   *    "org.h2.Driver",
   *    connString,
   *    "root",
   *    "root",
   *    true);
   *  jdbcStorageConfig.setEnabled(true);
   *  String pluginName = "h2";
   *  DrillbitContext context = cluster.drillbit().getContext();
   *  JdbcStoragePlugin jdbcStoragePlugin = new JdbcStoragePlugin(jdbcStorageConfig,
   *    context, pluginName);
   *  StoragePluginRegistryImpl pluginRegistry = (StoragePluginRegistryImpl) context.getStorage();
   *  pluginRegistry.addPluginToPersistentStoreIfAbsent(pluginName, jdbcStorageConfig, jdbcStoragePlugin);
   * @param args
   */
  public static void main(String[] args) {
    try (RemoteServiceSet serviceSet = RemoteServiceSet.getLocalServiceSet();) {
         DrillConfig config = DrillConfig.create();
         Drillbit drillbit = new Drillbit(config,serviceSet);
         //  启动Drill
          drillbit.run();

      // 获取Drill的配置对象和存储插件注册表
     // DrillConfig config = drillbit.getContext().getConfig();
      StoragePluginRegistry storagePluginRegistry = drillbit.getContext().getStorage();

      // 创建存储插件配置
      String name = "my_plugin";
      String type = "file";
      String connection = "file:///path/to/data/files";
      Object configObject = Class.forName(type + "Config").newInstance();
      configObject.getClass().getMethod("setName", String.class).invoke(configObject, name);
      configObject.getClass().getMethod("setConnection", String.class).invoke(configObject, connection);

      // 将存储插件配置添加到PersistentStoreProvider中
      PersistentStoreProvider storeProvider = storagePluginRegistry.getStore()
      storeProvider.getOrCreateStore(PersistentStoreProvider.CONFIGURATIONS).put(name, configObject);

      // 通知Drill重新加载配置
      drillbit.getContext().getOptionManager().setOption(ExecConstants.RELOAD_CONFIG, true);
      drillbit.getContext().getOptionManager().setOption(ExecConstants.RELOAD_PLUGIN, true);
      storagePluginRegistry.refreshAll(drillbit.getContext().getSchemaConfig());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}